package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.pojo.AppointmentTimeEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/10/12.
 */

public class ADA_SelectTimeRecycler extends RecyclerView.Adapter<ADA_SelectTimeRecycler.TimeViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<AppointmentTimeEntity.ListBean> mList;
    private int isSelected = -15;
    private OnItemClickListener itemClickListener;

    public ADA_SelectTimeRecycler(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setmList(List<AppointmentTimeEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public TimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TimeViewHolder(mInflater.inflate(R.layout.item_select_time_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(TimeViewHolder holder, final int position) {
        final AppointmentTimeEntity.ListBean listBean = mList.get(position);
        //设置时间
        holder.tvTime.setText(listBean.getTime());
        //根据点击事件做相应的变化
        if (isSelected == position){
            listBean.setSelected(true);
            holder.rlPresenter.setSelected(true);
            holder.tvTime.setSelected(true);
        }else{
            holder.rlPresenter.setSelected(false);
            holder.tvTime.setSelected(false);
        }

//        holder.rlPresenter.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                isSelected = position;
//                itemClickListener.onClick(position, listBean.getTime());
//                notifyDataSetChanged();
//            }
//        });
        //条目点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelected = position;
                itemClickListener.onClick(position, listBean.getTime(), listBean.getId(), listBean.getTechnicianName());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class TimeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.rl_presenter)
        RelativeLayout rlPresenter;
        public TimeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position, String time, String id, String name);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
